package com.tool;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

public class Convert
{
  public static void main(String[] args)
  {
    File dir = new File("./src");
    List list = new ArrayList();
    getJavaFiles(list, dir);
    for (int i = 0; i < list.size(); i++)
    {
      convert((File)list.get(i));
    }
    System.out.println("Convert Success!");
  }

  private static void convert(File file)
  {
    try
    {
      StringBuffer buf = new StringBuffer();
      BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charset.forName("GBK")));
      String line = null;
      while ((line = br.readLine()) != null)
      {
        buf.append(line).append("\r\n");
      }
      br.close();

      BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), Charset.forName("UTF-8")));
      out.append(buf.toString());
      out.flush();
      out.close();

      System.out.println("Convert File : " + file.getName() + " Success!");
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }

  private static void getJavaFiles(List list, File dir)
  {
    if (dir.isFile())
    {
      if (dir.getName().endsWith(".java"))
      {
        list.add(dir);
      }
    }
    else if (dir.isDirectory())
    {
      File[] files = dir.listFiles();
      for (int i = 0; i < files.length; i++)
      {
        getJavaFiles(list, files[i]);
      }
    }
  }
}